/*
 * usb_cmd_handler.c
 *
 *  Created on: Apr 28, 2024
 *      Author: wzx05
 */

// command_handler.c

#include "usb_cmd_handler.h"
#include "usbd_cdc_if.h" // 如果需要调用 CDC_Transmit_FS 等函数
#include "rfswctrl.h"

#define MAX_COMMAND_SIZE 8
static uint8_t commandBuffer[MAX_COMMAND_SIZE];
static uint32_t commandLength = 0;

static void processCommand(uint8_t* command, uint32_t length) {
	// 假设这里有处理命令的逻辑
	// 回显命令作为示例
	char response[512];
	//USBD_StatusTypeDef result1,result2;

	//snprintf(response, sizeof(response), "Command received: %.*s \r\n", (int)length, command);
	//result1 = CDC_Transmit_FS((uint8_t*)response, strlen(response));
	//TOFIX: Will always returen USBD_BUSY after the first command.
	/*
	HAL_Delay(10);
	memset(response, 0, sizeof(response));  // 清零整个数组
	strncpy(response, "Invalid RFSW \r\n", sizeof(response) - 1);
	response[sizeof(response) - 1] = '\0';  // 确保字符串以null字符结束
	result2 = CDC_Transmit_FS((uint8_t*)response, strlen(response));
	*/

    if (length != 2) {
        snprintf(response, sizeof(response), "Command Help: \"A1\" means switching RFSW A to position J1. \r\n");
        CDC_Transmit_FS((uint8_t*)response, strlen(response));
        return;
    }

    // 确保command[0]是有效的设备字符
    if (command[0] < 'A' || command[0] > ('A' + RFSW_Device_Count - 1)) {
        snprintf(response, sizeof(response), "Invalid RFSW Device. Use A to %c. \r\n", 'A' + RFSW_Device_Count - 1);
    	//memset(response, 0, sizeof(response));  // 清零整个数组
    	//strncpy(response, "Invalid RFSW2 \r\n", sizeof(response) - 1);
    	//response[sizeof(response) - 1] = '\0';  // 确保字符串以null字符结束
        CDC_Transmit_FS((uint8_t*)response, strlen(response));
        return;
    }

    uint8_t device = command[0] - 'A';  // 获取首字母，作为射频开关的编号
    uint16_t position = command[1] - '1';  // 获取第二个字符（数字），作为射频开关的位置

    if (position >= rfSwitchCtrlInfo.positionCount) {
        snprintf(response, sizeof(response), "Invalid RFSW position. Use 1 to N for SPNT.\r\n");
        CDC_Transmit_FS((uint8_t*)response, strlen(response));
        return;
    }

    snprintf(response, sizeof(response), "RFSwitch %c will switch to position %d.\r\n", device + 'A', position + 1);
    CDC_Transmit_FS((uint8_t*)response, strlen(response));

    setRFSwitch(device, position);

    snprintf(response, sizeof(response), "RFSwitch %c switched to position %d.\r\n", device + 'A', position + 1);
    CDC_Transmit_FS((uint8_t*)response, strlen(response));

}

void handleCommand(uint8_t* data, uint32_t length) {
	char response[512];

    for (uint32_t i = 0; i < length; i++) {
        if (data[i] == '\r' || commandLength >= MAX_COMMAND_SIZE) {

        	//snprintf(response, sizeof(response), "Command received: %.*s \r\n", (int)commandLength, commandBuffer);
            //CDC_Transmit_FS((uint8_t*)response, strlen(response));

            processCommand(commandBuffer, commandLength);
            commandLength = 0; // 重置命令缓冲区
        } else {
            commandBuffer[commandLength++] = data[i]; // 将数据添加到命令缓冲区
        }
    }
}

